/**
 * 下载excel文件流。
 * @param url 下载文件的url
 */
export default function downloadExcel(file, disposition) {
    if (!file) {
        throw new Error("download url is avalid!");
    }

    const blob = new Blob([file], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
        }),
        reader = new FileReader();
    let dispositions = disposition.match(/"(.*?)"/g),
        filename = dispositions && dispositions.length > 0 && dispositions[0].replace(/"/g, "");

    reader.readAsDataURL(blob);
    reader.onload = e => {
        const url = e.target.result,
            linkEl = document.createElement("a");

        if ("download" in linkEl) {
            // const filename = url.substr(url.lastIndexOf("/"));
            linkEl.download = filename || url.substr(url.lastIndexOf("/")) + ".xls";
            document.body.appendChild(linkEl);
            linkEl.href = url;
            linkEl.click();
            linkEl.remove();
        } else {
            const win = window.open(url, "_self");
            win.opener = null;
        }
    };
}
